<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\index\model;
use think\facade\Db;
use think\facade\Cookie;
//前台调用标签的模型函数
class TongjiModel{
    private $search_engine_inc=array(
        "baidu." =>"baidu",
        "google." =>"google",
        "sogou." =>"sogou",
        "so." =>"360",	
        "bing." =>"bing",
    );
    public function start(){
        $visit_userId =Cookie::get('visit_userId');
        if(!$visit_userId){
            $data=array();
            //进站方式
            $http_referer=$_SERVER['HTTP_REFERER'];
            if(!$http_referer){
                $data['visit_type']="website";
            }else{
                $searcn_engine=$this->search_engine();
                if($searcn_engine){
                    $data['visit_type']="search";
                    $data['search_engine']=$searcn_engine;
                }else{
                    $data['visit_type']="link";
                }
            }
            //ip地址
            $data['ip']=  get_client_ip(0,true);
            //访问时间
            $data['visit_time']=time();
            //先插入48小时的表
            Db::name("visit_48")->insert($data);
            //再插入visit表
            $today_start=strtotime(date("Y-m-d ")."00:00:00");
            $today_end=strtotime(date("Y-m-d ")."23:59:59");
            $where=array();
            $where[]=["visit_time","between",[$today_start,$today_end]];
            $data2=array();
            //时间
            $data2['daytime']=strtotime(date("Y-m-d"));                                    
            //IP统计            
            $ip_total=Db::name("visit_48")->field("ip")->where($where)->group("ip")->count(); 
            $data2['ip']=$ip_total;
            //pv统计
            $data2['pv']=Db::name("visit_48")->where($where)->count();    
            //visit_search 搜索进来的用户
            $where2=array();
            $where2=$where;
            $where2[]=["visit_type","=","search"];
            $data2['visit_search']=Db::name("visit_48")->where($where2)->count();  
            //website 输入网址进来的用户
            $where2=array();
            $where2=$where;
            $where2[]=["visit_type","=","website"];
            $data2['visit_website']=Db::name("visit_48")->where($where2)->count(); 
            //link 通过链接跳转进来的用户
            $where2=array();
            $where2=$where;
            $where2[]=["visit_type","=","link"];
            $data2['visit_link']=Db::name("visit_48")->where($where2)->count();
            //各大搜索引擎判断
            //baidu
            $where2=array();
            $where2=$where;
            $where2[]=["visit_type","=","search"];
            $where2[]=["search_engine","=","baidu"];
            $data2['engine_baidu']=Db::name("visit_48")->where($where2)->count();
            //google
            $where2=array();
            $where2=$where;
            $where2[]=["visit_type","=","search"];
            $where2[]=["search_engine","=","google"];
            $data2['engine_google']=Db::name("visit_48")->where($where2)->count();       
            //sogou
            $where2=array();
            $where2=$where;
            $where2[]=["visit_type","=","search"];
            $where2[]=["search_engine","=","sogou"];
            $data2['engine_sogou']=Db::name("visit_48")->where($where2)->count();    
            //360
            $where2=array();
            $where2=$where;
            $where2[]=["visit_type","=","search"];
            $where2[]=["search_engine","=","360"];
            $data2['engine_360']=Db::name("visit_48")->where($where2)->count();    
            //bing
            $where2=array();
            $where2=$where;
            $where2[]=["visit_type","=","search"];
            $where2[]=["search_engine","=","bing"];
            $data2['engine_bing']=Db::name("visit_48")->where($where2)->count();
            //
            $visit_id=Db::name("visit")->where(array("daytime"=>$data2['daytime']))->value("id");
            if($visit_id){
                $data2["id"]=$visit_id;
                Db::name("visit")->update($data2);
            }else{
                Db::name("visit")->insert($data2);
            }
            //设置访问cookie，过期时间为今天23:59:59
            Cookie::set("visit_userId",time()+rand(10000,99999), 86400);
        }
    }
    //判断搜索引擎
    private function search_engine(){
        foreach($this->search_engine_inc as $key=>$engine){
            if(strstr($_SERVER['HTTP_REFERER'],$key)){
                return $engine;
            }
        }
    }
}